<?php

namespace App\Services\Admin;

use App\Models\ClassBaseTime as ClassBaseTimeModel;

class ClassBaseTime extends Base
{
    public static function lists()
    {

        $where = [];

        $day = request()->post('day');
        $week = request()->get('week');

        if(!empty($week))$where[] = ['week',$week];

        if(!empty($day)){
            $week_day = date("N", strtotime("{$day}"));

            $where[] = ['week', $week_day];
        }

        return (new ClassBaseTimeModel)->where($where)->orderBy('week','asc')->orderBy('time_start','asc');

    }

    public static function info($id)
    {
        $class_base_time = (new ClassBaseTimeModel)->where('id', $id)->first();

        $time_start = explode(':', $class_base_time->time_start);

        $time_start_hour = $time_start[0];
        $time_start_minute = $time_start[1];

        $time_end = explode(':', $class_base_time->time_end);

        $time_end_hour = $time_end[0];
        $time_end_minute = $time_end[1];

        $result = [
            'time_start_hour' => $time_start_hour,
            'time_start_minute' => $time_start_minute,
            'time_end_hour' => $time_end_hour,
            'time_end_minute' => $time_end_minute,
            'week' => $class_base_time->week,
            'duration' => $class_base_time->duration,
        ];

        return $result;
    }

    public static function add($param = [])
    {
        return (new ClassBaseTimeModel)->insertGetId($param);
    }

    public static function edit($id , $param=[])
    {
        return (new ClassBaseTimeModel)->where('id',$id)->update($param);
    }

    public static function week_result()
    {

        $result = [
            '1'=>'星期一',
            '2'=>'星期二',
            '3'=>'星期三',
            '4'=>'星期四',
            '5'=>'星期五',
            '6'=>'星期六',
            '7'=>'星期日',
        ];

        return $result;
    }

    /**
     * 计算时长
     */
    public static function duration($start_hour_minute, $end_hour_minute)
    {
        $y_m_d = date("Y-m-d");
        $start = strtotime("{$y_m_d} {$start_hour_minute}");
        $end = strtotime("{$y_m_d} {$end_hour_minute}");

        $difference = $end-$start;

        return number_format($difference/3600,1);
    }
}
